﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Modules.Core;
using System.Windows.Controls;
using System.Data;
using Modules.Library.Entities;
using System.Windows;
using System.Collections;

namespace ComputerStoreManager.ViewModel {
    public class ViewProductControlViewModel : BaseProductControlViewModel {
        #region ICommand
        public ICommand PreviewCommand {
            get;
            private set;
        }
        public ICommand NextCommand {
            get;
            private set;
        }
        public ICommand ChangeProductInfoCommand {
            get;
            private set;
        }
        public ICommand SearchCommand {
            get;
            private set;
        }
        public ICommand ShowAllCommand {
            get;
            private set;
        }
        public ICommand SelectionChangedCommand {
            get;
            private set;
        }
        #endregion

        #region Variables
        private TextBox txtSearch;
        private ComboBox cbbTypeName;
        private ComboBox cbbSearchBy;
        private Button btnNext;
        private Button btnPreview;
        private Button btnPrintInvoice;
        private Button btnSearch;
        private Button btnShowAll;
        private DataGrid dataGrid1;
        private DataTable dataTemp;
        private Hashtable loaiMatHang;
        private int counter;

        #endregion

        #region Constructor
        public ViewProductControlViewModel(TextBox txtManufacturerName, TextBox txtNote, TextBox txtProductId,
            TextBox txtProductName, TextBox txtProviderName, TextBox txtPurchasePrice, TextBox txtRemaining,
            TextBox txtSalePrice, TextBox txtSearch, ComboBox cbbTypeName, ComboBox cbbSearchBy, Button btnNext,
            Button btnPreview, Button btnPrintInvoice, Button btnSearch, Button btnShowAll, DataGrid dataGrid1)
            : base(txtProductId, txtManufacturerName, txtProviderName, txtProductName, txtPurchasePrice, txtSalePrice, txtRemaining, txtNote) {
            // TODO: Complete member initialization
            this.txtSearch = txtSearch;
            this.cbbTypeName = cbbTypeName;
            this.cbbSearchBy = cbbSearchBy;
            this.btnNext = btnNext;
            this.btnPreview = btnPreview;
            this.btnPrintInvoice = btnPrintInvoice;
            this.btnSearch = btnSearch;
            this.btnShowAll = btnShowAll;
            this.dataGrid1 = dataGrid1;

            InitControlInfo();
            LoadData();
            InitCmd();

        }
        #endregion

        #region Init
        public void InitCmd() {
            PreviewCommand = new ExecutableCommand(DoPreview);
            NextCommand = new ExecutableCommand(DoNext);
            ChangeProductInfoCommand = new ExecutableCommand(DoChangeProductInfo);
            SearchCommand = new ExecutableCommand(DoSearch);
            ShowAllCommand = new ExecutableCommand(DoShowAll);
            SelectionChangedCommand = new ExecutableCommand(SelectionChangedEvent);
        }

        public override void InitControlInfo() {
            loaiMatHang = Model.GetLoaiMatHang();

            foreach(DictionaryEntry entry in loaiMatHang) {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = entry.Value;
                item.Tag = entry.Key;
                cbbTypeName.Items.Add(item);
            }
        }

        public override void DoClearFields() {
            //base.DoClearFields();
        }

        #endregion

        #region Methods
        public void DoChangeProductInfo() {
            try
            {
                if (CheckIsNotNull())
                {
                    Product product = new Product();
                    //product.AnhDaiDien
                    //product.ChiTietLinhKien
                    product.GhiChu = base.TxtNote.Text;
                    product.GiaBan = base.TxtPurchasePrice.Text;
                    product.GiaMua = base.TxtSalePrice.Text;
                    product.MaLoaiMatHang = ((ComboBoxItem)cbbTypeName.SelectedItem).Tag.ToString();
                    product.MaMatHang = base.TxtProductId.Text;
                    product.NhaCungCap = base.TxtProviderName.Text;
                    product.NhaSanXuat = base.TxtManufacturerName.Text;
                    product.SoLuongTonKho = Convert.ToInt32(base.TxtRemaining.Text);
                    product.TenMatHang = base.TxtProductName.Text;

                    if (!Model.UpdateData(product, KindOfObject.BY_PRODUCT))
                    {
                        MessageBox.Show("Lỗi");
                    }
                    else
                    {
                        MessageBox.Show("Thành Công");
                        LoadData();
                    }
                }
                else
                {
                    MessageBox.Show("Bạn không được bỏ trống bất cứ trường nào", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public void DoSearch() {
            try {
                var data = Model.SearchInfo(txtSearch.Text, (SearchingCondition)cbbSearchBy.SelectedIndex, KindOfObject.BY_PRODUCT);
                if(data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public void DoShowAll() {
            try {
                var data = Model.GetListLinhKien();
                if(data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoNext() {
            if(counter < dataTemp.Rows.Count - 1) {
                counter++;
            }
            LoadProductInfoByCounter(counter);
        }

        private void DoPreview() {
            if(counter > 0) {
                counter--;
            }
            LoadProductInfoByCounter(counter);
        }

        private void LoadData() {
            dataTemp = Model.GetListLinhKien();
            counter = 0;
            if(dataTemp != null && dataTemp.Rows.Count > 0) {
                LoadProductInfoByCounter(counter);
            }
        }

        private void SelectionChangedEvent(object obj) {
            try
            {
                if (dataTemp != null && dataTemp.Rows.Count > 0 && dataGrid1.SelectedIndex >= 0)
                {
                    LoadProductInfoByCounter(counter = dataGrid1.SelectedIndex);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " +ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void LoadProductInfoByCounter(int counter) {
            try
            {
                base.TxtManufacturerName.Text = dataTemp.Rows[counter]["TenNSX"].ToString();
                base.TxtNote.Text = dataTemp.Rows[counter]["GhiChu"].ToString();
                base.TxtProductId.Text = dataTemp.Rows[counter]["MaMH"].ToString();
                base.TxtProductName.Text = dataTemp.Rows[counter]["TenMH"].ToString();
                base.TxtProviderName.Text = dataTemp.Rows[counter]["TenNCC"].ToString();
                base.TxtPurchasePrice.Text = dataTemp.Rows[counter]["GiaBan"].ToString();
                base.TxtRemaining.Text = dataTemp.Rows[counter]["SoLuongTonKho"].ToString();
                base.TxtSalePrice.Text = dataTemp.Rows[counter]["GiaMua"].ToString();
                //cái này mày làm thêm nha :D làm trong converter load tên loại lên từ mã loại :D đuối rồi
                //txtTypeName.Text = dataTemp.Rows[counter]["SoDienThoai"].ToString();
                cbbTypeName.SelectedIndex = ConvertToComboBoxIndex(dataTemp.Rows[counter]["MaLoaiMH"].ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public void ReloadData() {
            LoadData();
        }
        private int ConvertToComboBoxIndex(string maLoaiMH) {
            int count = -1;
            foreach(DictionaryEntry entry in loaiMatHang) {
                count++;
                if(entry.Key.Equals(maLoaiMH)) {
                    return count;
                }
            }
            return 0;
        }

        #endregion
    }
}
